package com.baoyi.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.baoyi.doamin.ImageItem;
import com.baoyi.doamin.NewData;
import com.baoyi.service.ImageItemService;
import com.baoyi.widget.ListItemWidget;

public class MainDataAdapter extends BaseAdapter {

	private List<ListItemWidget> items;
	private List<ImageItem> datas;
	private Context context;
	private Page page;
	private int curpage;

	public MainDataAdapter(Context c) {
		context = c;
		NewData data = ImageItemService.FindNewIndex();
		datas = data.getImages();

		page = new Page();
		if (null != datas) {
			page.setTotalRows(data.getSize());
			page.setPageSize(10);
			page.setCurrentPage(1);
			page.config();
			items = new ArrayList<ListItemWidget>();
			for (ImageItem imageItem : datas) {
				items.add(new ListItemWidget(context, imageItem));
			}
		}
		curpage = 2;
	}

	public boolean add() {
		if (curpage > page.getTotalPages()) {
			return false;
		} else {
			List<ImageItem> temp = ImageItemService.page(curpage);
			curpage++;
			page.setCurrentPage(curpage);
			if(null!=temp){
				for (ImageItem imageItem : temp) {
					items.add(new ListItemWidget(context, imageItem));
				}
			}
			return true;
		}
	}

	public int getStar() {
		return 0;
	}

	public int getend() {
		return 0;
	}

	public int size() {
		if (null == datas) {
			return 0;
		} else {
			return datas.size();
		}
	}

	@Override
	public int getCount() {
		if (null == items) {
			return 0;
		} else {
			return items.size();
		}

	}

	@Override
	public Object getItem(int position) {
		return items.get(position).getImageItem();
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		return items.get(position);
	}

}
